﻿using Microsoft.Practices.CompositeWeb;
using ModulesToolkit.DataAccess.Membership;
using ModulesToolkit.DataContracts.Membership;

namespace ModulesToolkit.Membership.Views
{
    public class UsersListPresenter : Presenter<IUsersListView>
    {
        #region Fields

        private readonly UsersDtoRepository _usersRepository;
        private readonly RolesRepository _rolesRepository;

        #endregion

        #region Constructor

        public UsersListPresenter(UsersDtoRepository usersRepository, RolesRepository rolesRepository)
        {
            _usersRepository = usersRepository;
            _rolesRepository = rolesRepository;
        }

        #endregion

        #region Methods

        public override void OnViewLoaded()
        {
            // TODO: Implement code that will be executed every time the view loads
        }

        public override void OnViewInitialized()
        {
            // TODO: Implement code that will be executed the first time the view loads
        }

        public void OnUserListSelecting()
        {
            var searhCriteria = this.View.UsersSearchCriteria;
            searhCriteria.SortField = "UserName";
            var users = this._usersRepository.RetrieveUsers(searhCriteria);
            this.View.FillUsers(users);
           
        }

        public void OnLoadRolesList()
        {
            var entitySearchResult = this._rolesRepository.GetRoles(this.View.RolesSearchCriteria);
            this.View.FillRolesList(entitySearchResult);
        }

        public void OnSearchClick()
        {
            // bring data from database...bring the search criteria from view "interface"
            var entitySearchResult = this._usersRepository.RetrieveUsers(this.View.UsersSearchCriteria);

            // put the result from the database in the view "interface"
            this.View.FillUsers(entitySearchResult);
        }

        public void OnDeleteCommand(int id)
        {
            this._usersRepository.DeleteUser(id);
            this.OnSearchClick();

        }

        #endregion

       
    }
}




